home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / optasm / editor.asm < prev    next >
Encoding:
Assembly Source File  |  1993-06-09  |  9.0 KB  |  314 lines

  1. ;EDITOR.ASM -- Field input routine for use with
  2. ;              Microsoft QB 4.x, PDS 7.x & VB/DOS 1.x.
  3.  
  4. .Model Medium, Basic
  5. Extrn StringAddress:Proc      ;PDS, VB/DOS string
  6. Extrn StringLength:Proc       ;functions.
  7.  
  8. .Data
  9. CsrSize   DB   ?              ;Bottom scan line of cursor.
  10. InsFlag   DB   ?              ;Insert/overtype mode.
  11. MonSeg    DW   ?              ;Video segment.
  12.  
  13. .Code
  14. Proc Editor Uses DS ES DI SI, In$:Word, Start:Word, Row:Word, Col:Word, Colr:Word
  15.  
  16.      ;Local vars:
  17.      ;  Len    = LEN(In$)
  18.      ;  StrPtr = Offset of current char
  19.      ;  StrSeg = Segment In$
  20.      ;  StrOfs = Offset of In$
  21.      ;  Wide   = Screen width in bytes
  22.     
  23.      Local Len:Word, StrPtr:Word, StrSeg:Word, StrOfs:Word, Wide:Word
  24.  
  25.      Cmp  MonSeg, 0           ;MonSeg = 0?
  26.      Jnz  Mono                ;No, skip init code.
  27.      Mov  CsrSize, 12         ;Assume mono.
  28.      Mov  MonSeg, 0B000h
  29.      Xor  AX, AX
  30.      Mov  ES, AX              ;Point ES to low memory.
  31.      Mov  AL, ES:[463h]       ;Check monitor type.
  32.      Cmp  AL, 0B4h
  33.      Je   Mono
  34.      Mov  CsrSize, 7          ;Color monitor.
  35.      Add  MonSeg, 800h
  36.  
  37. Mono:
  38.      Mov  AH, 0Fh             ;Get video mode.
  39.      Push BP                  ;Some old BIOS's destroy
  40.      Int  10h                 ;BP during Int 10h.
  41.      Pop  BP                  ;Returns # columns in AH.
  42.      Mov  AL, AH              ;Put columns in AL.
  43.      Cbw                      ;Convert byte to word.
  44.      Shl  AX, 1               ;Double to get # bytes.
  45.      Mov  Wide, AX            ;Save it in Wide.
  46.  
  47.      Mov  SI, Start
  48.      Mov  AX, [SI]
  49.      Mov  StrPtr, AX          ;Save string pointer.
  50.      Cmp  StrPtr, 0           ;If StrPtr then StrPtr = StrPtr - 1
  51.      Jz   InitString
  52.      Dec  StrPtr
  53.     
  54. InitString:
  55.      Mov  SI, In$             ;Put descriptor address in SI.
  56.      Push SI
  57.      Call StringLength
  58.      Cmp  AX, 0               ;Is In$ null?
  59.      Jz   Shortcut            ;If so, exit.
  60.      Mov  Len, AX             ;Len = LEN(In$)
  61.      Push SI                  ;StringAddress returns
  62.      Call StringAddress       ;location of string data.
  63.      Mov  StrSeg, DX          ;Save segment
  64.      Mov  StrOfs, AX          ;and offset.
  65.  
  66.      ;============================================
  67.      ; To use with QB, replace the lines between
  68.      ; the InitString label and these comments
  69.      ; with the following:
  70.      ;============================================
  71.  
  72.      ;Mov  SI, In$             ;Put descriptor address in SI.
  73.      ;Mov  CX, [SI]            ;Put LEN(In$) in CX.
  74.      ;Jcxz Shortcut            ;Exit if length is zero.
  75.      ;Mov  Len, CX
  76.      ;Mov  DX, DS              ;Set StrSeg to default
  77.      ;Mov  StrSeg, DX          ;data segment.
  78.      ;Mov  CX, [SI+2]
  79.      ;Mov  StrOfs, CX          ;Save string offset.
  80.  
  81.      Call PrintString
  82.      Call SetCursor
  83.     
  84. GetKey:
  85.      Mov  AH, 2               ;LOCATE Row, Col + StrPtr
  86.      Xor  BH, BH              ;Point to video page 0.
  87.      Mov  SI, Col
  88.      Mov  DX, [SI]            ;Col in DX.
  89.      Dec  DX                  ;Convert 1-80 to 0-79.
  90.      Add  DX, StrPtr          ;Add StrPtr.
  91.      Mov  SI, Row
  92.      Mov  DH, [SI]            ;Row in DH.
  93.      Dec  DH                  ;Convert 1-25 to 0-24.
  94.      Push BP
  95.      Int  10h
  96.      Pop  BP
  97.  
  98.      Call WaitKey             ;Get a key.
  99.      Cmp  AL, 0               ;If AL = 0, then an
  100.      Jz   Extended            ;extended key was pressed.
  101.      Xor  AH, AH              ;Clear AH so that AX will
  102.                               ;contain std key code.
  103.      Cmp  AL, 13              ;Exit if Enter or
  104.      Je   Shortcut            ;Escape are pressed.
  105.      Cmp  AL, 27
  106.      Je   Shortcut
  107.      Cmp  AL, 8               ;Handle Backspace here.
  108.      Jne  Alpha               ;Process other keys at Alpha.    
  109.      Cmp  StrPtr, 0           ;Can't Backspace if already
  110.      Jz   GetKey              ;at first character!    
  111.      Dec  StrPtr              ;StrPtr = StrPtr - 1
  112.      Jmp  Shift               ;Shift text to the left.
  113.                               ;(see Delete code below)
  114. Repeat:
  115.      Call PrintString
  116.      Jmp  GetKey
  117. Extended:
  118.      Jmp  ExtKey
  119. Shortcut:
  120.      Jmp  Exit
  121.     
  122. Alpha:
  123.      Cmp  AL, 32
  124.      Jb   Shortcut
  125.      Mov  CX, Len
  126.      Cmp  StrPtr, CX          ;If StrPtr < (Len + 1)...
  127.      Jnb  Repeat
  128.      Inc  StrPtr              ;StrPtr = StrPtr + 1
  129.     
  130.      Mov  BX, StrSeg
  131.      Mov  ES, BX              ;Set ES to In$ segment.
  132.      Cmp  InsFlag, 0          ;If not insert mode,
  133.      Jz   PrintChar           ;skip the next part.
  134.     
  135.      Push ES                  ;Set DS to ES.
  136.      Pop  DS
  137.      Mov  SI, StrOfs          ;Point DS:SI at penultimate
  138.      Add  SI, CX              ;character of In$.
  139.      Dec  SI
  140.      Dec  SI
  141.      Mov  DI, SI              ;Set DI to SI + 1.
  142.      Inc  DI
  143.      Mov  BX, StrPtr
  144.      Sub  CX, BX              ;Copy Len - StrPtr chars.
  145.      Std                      ;Copy from right to left.
  146.      Rep  Movsb               ;Do it!
  147.      Push SS                  ;Reset DS.
  148.      Pop  DS
  149.      Cld                      ;Clear direction flag.
  150.     
  151. PrintChar:
  152.      Mov  DI, StrOfs
  153.      Add  DI, StrPtr
  154.      Dec  DI
  155.      Mov  ES:[DI], AL
  156.      Jmp  Repeat
  157.     
  158. ExtKey:
  159.      Mov  AL, AH              ;Convert extended key
  160.      Cbw                      ;into negative code in AX.
  161.      Neg  AX
  162.  
  163. Home:
  164.      Cmp  AX, -71
  165.      Jne  EndKey
  166.      Mov  StrPtr, 0
  167.      Jmp  Repeat
  168.     
  169. EndKey:
  170.      Cmp  AX, -79
  171.      Jne  Left
  172.      Push AX                  ;Save AX (key code).
  173.      Mov  BX, StrSeg
  174.      Mov  ES, BX              ;Point ES:DI at last char.
  175.      Mov  DI, StrOfs
  176.      Mov  CX, Len             ;Length in CX.
  177.      Add  DI, CX
  178.      Dec  DI
  179.      Std                      ;Search backward
  180.      Mov  AL, 32              ;for a non-space.
  181.      Repe Scasb
  182.      Jcxz NotFound
  183.      Inc  CX
  184. NotFound:
  185.      Mov  StrPtr, CX
  186.      Cld
  187.      Pop  AX
  188.      Jmp  Repeat
  189.     
  190. Left:
  191.      Cmp  AX, -75
  192.      Jne  Right
  193.      Cmp  StrPtr, 0
  194.      Jz   GoBack
  195.      Dec  StrPtr
  196.      Jmp  Repeat
  197.     
  198. Right:
  199.      Cmp  AX, -77
  200.      Jne  Insert
  201.      Mov  BX, Len
  202.      Cmp  StrPtr, BX
  203.      Jnb  GoBack
  204.      Inc  StrPtr
  205.      Jmp  Repeat
  206.     
  207. Insert:
  208.      Cmp  AX, -82
  209.      Jne  Delete
  210.      Mov  BL, InsFlag
  211.      Xor  BL, -1
  212.      Mov  InsFlag, BL
  213.      Call SetCursor
  214.      Jmp  Repeat
  215.     
  216. Delete:
  217.      Cmp  AX, -83
  218.      Jne  GoBack
  219. Shift:
  220.      Mov  BX, StrSeg
  221.      Mov  DS, BX              ;Point DS:SI at StrPtr + 1.
  222.      Mov  SI, StrOfs
  223.      Mov  BX, StrPtr
  224.      Inc  BX
  225.      Add  SI, BX
  226.      Push DS                  ;Set ES to DS.
  227.      Pop  ES
  228.      Mov  DI, SI              ;Set DI to SI - 1.
  229.      Dec  DI
  230.      Mov  CX, Len             ;Copy Len - StrPtr chars.
  231.      Sub  CX, BX
  232.      Cld                      ;Copy forward.
  233.      Rep  Movsb               ;Do it!
  234.      Mov  Byte Ptr ES:[DI], 32
  235.      Push SS                  ;Reset DS.
  236.      Pop  DS
  237. GoBack:
  238.      Jmp  Repeat
  239.  
  240. Exit:
  241.      Mov  BX, StrPtr          ;Put StrPtr + 1
  242.      Inc  BX                  ;in Start parameter
  243.      Mov  SI, Start           ;(AX contains code of
  244.      Mov [SI], BX             ;last keystroke).
  245.      Ret
  246.  
  247. Editor Endp
  248. PrintString Proc Near
  249.  
  250.      Push DS
  251.      Mov  SI, Row
  252.      Mov  AX, [SI]            ;Put current row number into AX.
  253.      Dec  AX                  ;Convert 1-25 to 0-24.
  254.      Mov  BX, Wide            ;Multiply by width to get row address.
  255.      Mul  BX                  ;Result ends up in AX.
  256.  
  257.      Mov  SI, Col
  258.      Mov  BX, [SI]            ;Same deal for column.
  259.      Dec  BX
  260.      Shl  BX, 1               ;Double it for chars and attributes.
  261.      Add  AX, BX              ;Add to base address.
  262.  
  263.      Mov  BX, MonSeg
  264.      Mov  ES, BX              ;Point ES to video segment
  265.      Mov  DI, AX              ;and DI to screen address.
  266.      Mov  SI, Colr
  267.      Mov  AH, [SI]            ;Set AH to color attribute.
  268.      Mov  BX, StrSeg
  269.      Mov  DS, BX
  270.      Mov  SI, StrOfs          ;Point DS:SI to In$
  271.      Mov  CX, Len             ;and string length in CX.
  272.      Cld                      ;Copy forward.
  273.  
  274. PutChar:
  275.      Lodsb
  276.      Stosw
  277.      Loop PutChar
  278.      Pop  DS
  279.      Ret
  280.  
  281. PrintString Endp
  282. SetCursor Proc Near
  283.  
  284.      Xor  CH, CH              ;Set top of cursor to 0.
  285.      Cmp  InsFlag, 0          ;InsFlag = 0?
  286.      Jnz  InsMode             ;If not, skip ahead.
  287.      Mov  CH, CsrSize         ;Top cursor scan line
  288.      Dec  CH                  ;(CsrSize - 1) in CH.
  289.  
  290. InsMode:
  291.      Mov  CL, CsrSize         ;Set bottom to CsrSize.
  292.      Push AX                  ;Save AX (key code)
  293.      Push BP
  294.      Mov  AH, 1               ;Set cursor size.
  295.      Int  10h
  296.      Pop  BP
  297.      Pop  AX
  298.      Ret
  299.  
  300. SetCursor Endp
  301. WaitKey Proc Near
  302.  
  303.      Xor  AX, AX
  304.      Mov  ES, AX              ;Clear keyboard buffer:
  305.      Mov  AX, ES:[41Ah]       ;  Get kybd buffer head ptr
  306.      Mov  ES:[41Ch], AX       ;  Set tail equal to head
  307.      Mov  AH,0                ;Wait for a keystroke.
  308.      Int  16h                 ;Key code returned in AX.
  309.      Ret
  310.  
  311. WaitKey Endp
  312. End
  313.  
  314.